
local actorPropertiesDialog = nil
local attachActorButton = nil
local playButton = nil
local facePanel = nil

local LIST_PADDING = 2
local SCROLLBAR_WIDTH = 7
local PADDING = 7
local function CreateLabel(text, color, x,y, parent)
	local label = vgui.Create("DLabel", parent) 
	label:SetText(text) 
	label:SetTextColor(color) 
	label:SizeToContents()
	label:SetPos(x,y)
	return label
end
local WHITE = Color(255,255,255,255)
local GREY = Color(32,32,32,255)

local function LoadFacesListPanel(panel, actorEntity)
	local itemDimen = (180 - (2*PADDING) - (2*LIST_PADDING) - LIST_PADDING - SCROLLBAR_WIDTH) / 2

	panel:Clear()
	local FACE_DIR = "../materials/myproject/actors/"
	local headFn = file.Find(FACE_DIR .. "*.vmt")
	for _, f in pairs(headFn) do
		local filename = string.sub(f,1, string.len(f) - 4)
		local mateiralFilepath = FACE_DIR .. filename

		local headButton = vgui.Create( "DImageButton") 
		headButton:SetMaterial( mateiralFilepath ) 
		headButton:SetSize(itemDimen,itemDimen)
		headButton.DoClick = function () 
		print(actorEntity:GetClass())
		actorEntity:SetMaterial(mateiralFilepath)
//actorEntity:SetSkin(1)

		
		
		end

		panel:AddItem(headButton)
	end

end

local function ShowActorProperties(userMessage)

	local callingEntity = userMessage:ReadEntity()
	local screenPos = callingEntity:GetPos():ToScreen()

	if(actorPropertiesDialog) then
		actorPropertiesDialog:SetVisible(true)
	else
		local ANCHOR_Y = PADDING
		local ANCHOR_X = PADDING
		actorPropertiesDialog = vgui.Create( "DFrame" ) 
		actorPropertiesDialog:SetPos( screenPos.x + 30,screenPos.y - 300) 
		actorPropertiesDialog:SetSize( (2 * PADDING) + 180, 330 ) 
		actorPropertiesDialog:SetTitle( "Properties")
		actorPropertiesDialog:SetVisible( true ) 
		actorPropertiesDialog:SetDraggable( false )
		actorPropertiesDialog:ShowCloseButton( false )
		actorPropertiesDialog:MakePopup() 
	    
		ANCHOR_Y = ANCHOR_Y + 20

		CreateLabel("Appearance:", WHITE, ANCHOR_X, ANCHOR_Y, actorPropertiesDialog)
		ANCHOR_Y = ANCHOR_Y + 20
		ANCHOR_X = ANCHOR_X + PADDING

		facePanel = vgui.Create("DPanelList")
		facePanel:SetParent(actorPropertiesDialog)
		facePanel:EnableHorizontal(true)
		facePanel:EnableVerticalScrollbar(true)
		facePanel:SetPos(ANCHOR_X,ANCHOR_Y)
		facePanel:SetPadding(LIST_PADDING)
		facePanel:SetSpacing(LIST_PADDING)
		facePanel:SetSize(180 - (2*PADDING) , 240)

	    LoadFacesListPanel(facePanel, callingEntity)

		//headButton = vgui.Create( "DImageButton") 
		//headButton:SetMaterial( "gui/control_stop" ) 
		//headButton:SetSize(itemDimen,itemDimen)
		//headPanel:AddItem(headButton)

		//headButton = vgui.Create( "DImageButton") 
		//headButton:SetMaterial( "gui/control_stop" ) 
		//headButton:SetSize(itemDimen,itemDimen)
		//headPanel:AddItem(headButton)

		ANCHOR_Y = ANCHOR_Y + 240 + PADDING
		ANCHOR_X = ANCHOR_X - PADDING

		playButton = vgui.Create( "DButton" ) 
		playButton:SetParent( actorPropertiesDialog ) // Set parent to our "DermaPanel" 
		playButton:SetText( "Close" ) 
		playButton:SetPos( ANCHOR_X, ANCHOR_Y) 
		playButton:SetSize( 180, 25 ) 
		ANCHOR_Y = ANCHOR_Y + 30

	end   

	if facePanel then
		LoadFacesListPanel(facePanel, callingEntity)
	end

	if playButton then
		playButton.DoClick = function () 
			actorPropertiesDialog:SetVisible(false)
		end
	end

end

usermessage.Hook( "ShowProperties".."actor", ShowActorProperties) 
